package com.yupi.yuaiagent.tool;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import com.yupi.yuaiagent.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

//根据资源地址下载资源
public class ResourceDownloadTool {

    @Tool(description = "Download resource from url")
    public String downloadResource(@ToolParam(description = "URL of the resource to download") String url,
                                   @ToolParam(description = "Name of the file to save the download resource") String fileName) {
        String fileDir = FileConstant.FILE_SAVE_DIR + "/download/";
        String filePath = fileDir + fileName;
        try {
            //创建目录
            FileUtil.mkdir(fileDir);
            //下载资源
            HttpUtil.downloadFile(url, filePath);
            return "Resource download success to: " + filePath;
        } catch (Exception e) {
            return "Resource download failed: " + e.getMessage();
        }
    }
}